package com.ruoyi.system.amqp;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ProductMatcher {

    @Autowired
    private ISysConfigService configService;

    public final int TEMPERATURE_SENSOR = 1;

    public final int LINE_QUERY = 2;

    public final int LINE_CONTROL = 3;

    private String temperatureSensor;
            // "k0yrr8izMtu";

    private String lineQuery;
            // "k0yrr7rtEXu";

    private String lineControl;
            // "k0yrrTVK6bO";

    public boolean isTemperatureSensor(String topic) {
        return topic.matches(".*"+temperatureSensor+".*");
    }
    public boolean isLineQuery(String topic) {
        return topic.matches(".*"+lineQuery+".*");
    }
    public boolean isLineControl(String topic) {
        return topic.matches(".*"+lineControl+".*");
    }

    public int getTopicType(String topic) {
        if (isTemperatureSensor(topic)) {
            return TEMPERATURE_SENSOR;
        } else if (isLineQuery(topic)) {
            return LINE_QUERY;
        } else if (isLineControl(topic)) {
            return LINE_CONTROL;
        } else {
            return -1;
        }
    }


    public ProductMatcher() {
    }

    @PostConstruct
    public void init(){
        temperatureSensor = selectProductKeyByProductName("温度检测模块");
        lineQuery = selectProductKeyByProductName("请求查询产线其他设备");
        lineControl = selectProductKeyByProductName("请求控制产线其他设备");
        if(StringUtils.isEmpty(temperatureSensor) || StringUtils.isEmpty(lineQuery) || StringUtils.isEmpty(lineControl)){
            throw new ServiceException("阿里云产品Key未配置，请检查sys_config表中sys.device.配置项的值是否配置\n" +
                    "具体配置项如下：\n" +
                    "温度检测模块：sys.device.温度检测模块\n" +
                    "请求查询产线其他设备：sys.device.请求查询产线其他设备\n" +
                    "请求控制产线其他设备：sys.device.请求控制产线其他设备");
        }
    }

    /**
     * 获取产品ProductKey
     *
     * @param ProductName 产品名称
     * @return 产品ProductKey
     */
    public String selectProductKeyByProductName(String ProductName)
    {
        if (StringUtils.isEmpty(ProductName))
        {
            throw new ServiceException("产品名称不能为空");
        }
        String productKey = null;
        if (configService != null) {
            productKey = configService.selectConfigByKey("sys.device." + ProductName);
        }
        if (StringUtils.isEmpty(productKey))
        {
            throw new ServiceException("产品Key未配置");
        }
        return productKey;
    }

    public String getProductKey(Integer topicType) {
        if (topicType == TEMPERATURE_SENSOR) {
            return temperatureSensor;
        } else if (topicType == LINE_QUERY) {
            return lineQuery;
        } else if (topicType == LINE_CONTROL) {
            return lineControl;
        } else {
            return null;
        }
    }
}
